﻿using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace TD.TheGame
{
    public abstract class EnemySprite : Sprite
    {
        private string assetName;

        public int Health
        {
            get;
            set;
        }

        public int MaxSpeed
        {
            get;
            set;
        }

        //speed of the sprite
        public int Speed
        {
            get;
            set;
        }

        //Damage of the Sprite
        public int Damage
        {
            get;
            set;
        }

        protected Rectangle[] wlk; // Walk sequence
        protected Rectangle[] att; // Attack sequence
        protected Rectangle[] die; // Dying sequence
        protected Rectangle[] dieAlternative;
        Random random = new Random();
        int yDifference = 15;

        public EnemySprite(Game game, Vector2 position, bool active, int maxLoopTime, SpriteEnumState state, int speed) :
            base(game, position, active, maxLoopTime, state)
        {
            this.assetName = getAssetName();

            // Assign the rectangles.
            string path = (Environment.CurrentDirectory + "\\XML\\Enemies\\" + assetName + "\\" + assetName);
            wlk = animationHandler.animationRec(path + "Walk.xml");
            att = animationHandler.animationRec(path + "Attack.xml");
            die = animationHandler.animationRec(path + "Die.xml");
            if (att.Length == 1)
            {
                att = wlk;
            }
            //If there is no die animation, Init one
            if (die.Length == 1)
            {
                dieAlternative = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\Enemies\\Explosion\\Explode.xml");
                die = dieAlternative;
            }

            // Make the default animation to be walk.
            current = wlk;
            MaxSpeed = speed;
            Speed = speed;
        }

        public override void Update(GameTime gameTime)
        {
            if (Active)
            {
                // Check if the state has changed.
                if (oldState != State)
                {
                    // Set the animation to the proper sequence.
                    if (State == SpriteEnumState.Die)
                    {
                        current = die;
                    }
                    else if (State == SpriteEnumState.Normal)
                    {
                        current = wlk;
                    }
                    else if (State == SpriteEnumState.Shoot)
                    {
                        current = att;
                    }
                    else
                    {
                        throw new NotSupportedException("The state to which the sprite has changed is not supported by the EnemySprite class.");
                    }

                    // Reset the current frame to zero.
                    currentRect = 0;

                    // Store the new state so we can compare it in the future.
                    oldState = State;
                }
                    MoveEnemy();
                    base.Update(gameTime);
                    Die();    
            }
        }

        //Needs to be abstract 
        private void MoveEnemy()
        {
            position.X -= Speed;
        }

        private void Die()
        {
            if (Health <= 0)
            {
                if (State != SpriteEnumState.Die)
                {
                    State = SpriteEnumState.Die;
                    Speed = 0;
                    if (die == dieAlternative)
                    {
                        maxLoopTime = 100;
                        texture = getDieTexture();
                        position.Y -= 40;
                    }
                }
                if (endSprite)
                {
                    boundingRectangle.X = 0;
                    boundingRectangle.Y = 0;
                    position.X = Game.Window.ClientBounds.Width;
                    position.Y = Game.Window.ClientBounds.Height - random.Next(6, 12) * yDifference;
                    Active = false;
                }
            }
        }

        public override void Reset()
        {
            current = wlk;
        }

        protected override Texture2D getTexture()
        {
            // NOTE: I find this a rather ugly solution.
            string assetName = ("level\\enemies\\" + getAssetName());
            manager.LoadAsset(assetName);
            return manager.UseAsset(assetName);
        }

        protected abstract string getAssetName();

        private Texture2D getDieTexture()
        {
            string assetName = ("level\\enemies\\" + "explosion");
            manager.LoadAsset(assetName);
            return manager.UseAsset(assetName);
        }
    }
}
